package cn.yunhe.control;

import cn.yunhe.entity.User;
import cn.yunhe.server.UserServer;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码格式
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        UserServer userServer = new UserServer();//实例化userserver对象
        //获取用户账号和密码
        String username = request.getParameter("username");
        String password = request.getParameter("pwd");
        String remember = request.getParameter("remember");

        HttpSession session = request.getSession(); //获取session对象
        //将账号密码保存到session对象中
        session.setAttribute("username", username);
        session.setAttribute("password", password);

        User user = new User(username, password); //实例化用户对象

        User user1 = userServer.login(user); //调用注册方法,获取返回值,根据返回值判断用户类型
        boolean flag = userServer.checkUserName(username); //检验用户名
        if (!flag) {
            request.setAttribute("result", "该用户不存在!!");
            request.getRequestDispatcher("login.jsp").forward(request, response); //跳转到主页面
        }else if (user1 == null){
            request.setAttribute("result", "密码错误!!");
            request.getRequestDispatcher("login.jsp").forward(request, response); //跳转到主页面
        } else {
            session.setAttribute("user",user1);  //将用户对象存入session
            if (remember != null){
                Cookie cookie1 = new Cookie("username", username);
                Cookie cookie2 = new Cookie("password", password);
                cookie1.setMaxAge(60 * 60 * 1);
                cookie2.setMaxAge(60 * 60 * 1);
                response.addCookie(cookie1);
                response.addCookie(cookie2);
            }
            if (user1.getFlag() == 1) {
                request.setAttribute("result", "登录成功!!");
                request.getRequestDispatcher("admin.jsp").forward(request, response); //跳转到管理员界面
            } else if (user1.getFlag() == 0) {
                if (user1.getuState() == 0) {
                    request.setAttribute("result", "该用户被禁言!!");
                    request.getRequestDispatcher("login.jsp").forward(request, response); //跳转到主页面
                } else if (user1.getuState() == 1) {
                    request.setAttribute("result", "登录成功!!");
                    request.getRequestDispatcher("user.jsp").forward(request, response); //跳转到普通用户界面
                } else {
                    request.setAttribute("result", "登录失败!!");
                    request.getRequestDispatcher("login.jsp").forward(request, response); //跳转到主页面
                }
            } else {
                request.setAttribute("result", "登录失败!!");
                request.getRequestDispatcher("login.jsp").forward(request, response); //跳转到主页面
            }
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }


}
